﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;

namespace com.vs.Shipping.BL
{
    public class ShipmentManager
    {
        public string SaveShipmentHdr(Session sess, ShipmentHeaderDO shpHdrDO)
        {
            string stErr = null;
            int iCnt = 0;
            DataHelper helper = null;
            string stSQL = null;

            try
            {
                if (shpHdrDO.INTERNAL_SHIPMENT_NUM > 0)
                {
                    shpHdrDO.Update(sess);
                    stErr = "" + shpHdrDO.INTERNAL_SHIPMENT_NUM;
                }
                else
                {
                    //检查是否存在相同单号的出库单
                    stSQL = " SELECT COUNT(1) FROM SHIPMENT_HEADER WHERE SHIPMENT_ID = @SHIPMENT_ID AND WAREHOUSE = @WAREHOUSE ";
                    helper = new DataHelper(sess);

                    iCnt = helper.GetInt(CommandType.Text, stSQL, new IDataParameter[]{
                        DataHelper.BuildParameter(sess, "@SHIPMENT_ID", shpHdrDO.SHIPMENT_ID),
                        DataHelper.BuildParameter(sess, "@WAREHOUSE", shpHdrDO.WAREHOUSE)});

                    if (iCnt > 0)
                        return MessageManager.MSG_SHP_007;//错误：相同单号的出库单已经存在


                    stErr = "" + shpHdrDO.Insert(sess);
                    if (StringManager.IsEmpty(stErr))
                        HistoryManager.SaveProcessHistory(sess, 
                            HistoryManager.ProcessType.iShipment, 
                            HistoryManager.Condition.iNew, 
                            "出库单创建成功", 
                            shpHdrDO.SHIPMENT_ID, sess.User, null, null);
                }

                return stErr;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        public string DeleteShipment(Session sess, int iIntShpNum)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                return helper.GetString(CommandType.StoredProcedure, "SHP_DShipmentHeader", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_SHIPMENT_NUM", iIntShpNum)});
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string AddToWave(Session sess, int iIntShpNum, int iWaveNum)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                return helper.GetString(System.Data.CommandType.StoredProcedure, "SHP_AddToWave", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_SHIPMENT_NUM", iIntShpNum),
                    DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum)});
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        public string AddToWave(Session sess, int[] arIntShpNum, int iWaveNum)
        {
            DataHelper helper = null;
            StringBuilder sbErr = null;
            string stErr = null;

            try
            {
                helper = new DataHelper(sess);
                foreach (int iIntShpNum in arIntShpNum)
                {
                    stErr = helper.GetString(System.Data.CommandType.StoredProcedure, "SHP_AddToWave", new IDataParameter[]{
                            DataHelper.BuildParameter(sess, "@INTERNAL_SHIPMENT_NUM", iIntShpNum),
                            DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum)});
                    if (!StringManager.IsEmpty(stErr))
                        sbErr.AppendLine(stErr);
                }
                if (!StringManager.IsEmpty(sbErr))
                    return sbErr.ToString();
                else
                    return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }
        public string AssignToLoad(Session sess, int iIntLoadNum, int iIntShpNum)
        {
            DataHelper helper = null;
            string stErr = null;

            try
            {
                //检查出库单和装载是否存在，状态是否可以操作
                helper = new DataHelper(sess);
                stErr = helper.GetString(CommandType.StoredProcedure, "SHP_VAssignToLoad", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_SHIPMENT_NUM", iIntShpNum),
                    DataHelper.BuildParameter(sess, "@INTERNAL_LOAD_NUM", iIntLoadNum)});
                if (!StringManager.IsEmpty(stErr))
                    return stErr;

                stErr = helper.GetString(CommandType.StoredProcedure, "SHP_AssignToLoad", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_SHIPMENT_NUM", iIntShpNum),
                    DataHelper.BuildParameter(sess, "@INTERNAL_LOAD_NUM", iIntLoadNum)});
                if (!StringManager.IsEmpty(stErr))
                    return stErr;
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string RemoveFromLoad(Session sess, int iIntLoadNum, int iIntShpNum)
        {
            DataHelper helper = null;
            string stErr = null;

            try
            {
                helper = new DataHelper(sess);
                stErr = helper.GetString(CommandType.StoredProcedure, "SHP_VRemoveFromLoad", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_LOAD_NUM", iIntLoadNum),
                    DataHelper.BuildParameter(sess, "@INTERNAL_SHIPMENT_NUM", iIntShpNum)
                });
                if (!StringManager.IsEmpty(stErr))
                    return stErr;


                helper.Update(CommandType.StoredProcedure, "SHP_RemoveFromLoad", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_LOAD_NUM", iIntLoadNum),
                    DataHelper.BuildParameter(sess, "@INTERNAL_SHIPMENT_NUM", iIntShpNum)
                });
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string CreateShipmentDtl(Session sess, ShipmentDetailDO sdDO)
        {
            return null;
        }

        public string DeleteShipmentDtl(Session sess, int iIntShpLnNum)
        {
            DataHelper helper = null;
            string stErr = null;

            try
            {
                helper = new DataHelper(sess);
                stErr = helper.GetString(CommandType.StoredProcedure, "SHP_DShipmentDetail", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_SHIPMENT_LINE_NUM", iIntShpLnNum)});
                if (StringManager.IsEmpty(stErr))
                    return null;
                else
                    return stErr;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string UpdateShipmentDtl(Session sess, ShipmentDetailDO sdDO)
        {
            return null;
        }

        public string UpdateShipmentStatusByWave(Session sess, int iWaveNum)
        {
            DataHelper helper = null;
            string stSQL = "SHP_UShipmentStatusByWave";

            try
            {
	            helper = new DataHelper(sess);
	            helper.Update(CommandType.StoredProcedure, stSQL, new IDataParameter[] { 
				            DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum),
			            });
                return null;
            }
            catch (Exception e)
            {
	            ExceptionManager.LogException(sess, e);
	            return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
	            if (helper != null)
		            helper.Dispose();
            }
        }

        public string UpdateShipmentStatus(Session sess, int iShpNum, int iLeadingSts, int iTrailingSts)
        {
            ShipmentHeaderDO shDO = null;
            ShippingLoadDO slDO = null;
            Dictionary<string, object> dcValue = null;
            ShippingLoadManager mgr = null;

            try
            {
                shDO = new ShipmentHeaderDO();
                shDO.INTERNAL_SHIPMENT_NUM = iShpNum;
                dcValue = new Dictionary<string, object>();
                if (iLeadingSts > 0)
                    dcValue.Add("LEADING_STS", iLeadingSts);
                if (iTrailingSts > 0)
                    dcValue.Add("TRAILING_STS", iTrailingSts);

                if (dcValue.Count > 0)
                {
                    shDO.Update(sess, dcValue);
                    if (shDO.INTERNAL_LOAD_NUM > 0)
                    {
                        mgr = new ShippingLoadManager();
                        return mgr.UpdateLoadStatus(sess, shDO.INTERNAL_LOAD_NUM);
                    }
                }
                return null;

            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        public string UpdateShipmentStatus(Session sess, int iShpNum)
        {
            DataHelper helper = null;
            string stSQL = "SHP_UShipmentStatus";

            try
            {
                //更新行状态
                //更新整单状态
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, new IDataParameter[] { 
				            DataHelper.BuildParameter(sess, "@INTERNAL_SHIPMENT_NUM", iShpNum),
			            });
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public DataTable GetShipmentHdrsByQFCri(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy, ref string stError)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                sbSQL = new StringBuilder(" SELECT DISTINCT SH.* FROM SHIPMENT_HEADER SH LEFT JOIN SHIPMENT_DETAIL SD ON SH.INTERNAL_SHIPMENT_NUM = SD.INTERNAL_SHIPMENT_NUM WHERE 1=1 ");
                helper = new DataHelper(sess);
                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key.ToString()).Append(de.Value.Key).Append("@Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value.Value);
                    i++;
                }

                if (!StringManager.IsEmpty(stOrderBy))
                {
                    sbSQL.Append(stOrderBy);
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                stError = MessageManager.MSG_GEN_000 + e.Message;
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

    }
}
